<?php 
class ControllerDesignBanner extends Controller {
	private $error = array();
 
	public function index() {
		$this->load->language('design/banner');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('design/banner');
		$this->getList();
	}

	public function insert() {
		$this->load->language('design/banner');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('design/banner');
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->model_design_banner->addBanner($this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('design/banner', 'token=' . $this->session->data['token'], 'SSL'));
		}
		$this->getForm_text();
		$this->getForm_insert();
	}

	public function update() {
		$this->load->language('design/banner');
		$this->document->setTitle($this->language->get('heading_title'));
		$this->load->model('design/banner');
		if ($this->request->server['REQUEST_METHOD'] == 'POST') {
			$this->model_design_banner->editBanner($this->request->get['banner_id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$this->redirect($this->url->link('design/banner', 'token=' . $this->session->data['token'], 'SSL'));
		}
		$this->getForm_text();
		$this->getForm_update();
	}
 
	private function getList() {
		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('design/banner', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
		$this->data['insert'] = $this->url->link('design/banner/insert', 'token=' . $this->session->data['token'], 'SSL');
		
		$url = '';
  		if (isset($this->request->get['filter_name'])) {
			$filter_name = $this->request->get['filter_name'];
			$url .= '&filter_name=' . $this->request->get['filter_name'];
		} else {
			$filter_name = null;
		}
  		if (isset($this->request->get['filter_position_type'])) {
			$filter_position_type = $this->request->get['filter_position_type'];
			$url .= '&filter_position_type=' . $this->request->get['filter_position_type'];
		} else {
			$filter_position_type = null;
		}
  		if (isset($this->request->get['filter_status'])) {
			$filter_status = $this->request->get['filter_status'];
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		} else {
			$filter_status = null;
		}
		if (isset($this->request->get['filter_position_name'])) {
			$filter_position_name = $this->request->get['filter_position_name'];
			$url .= '&filter_position_name=' . $this->request->get['filter_position_name'];
		} else {
			$filter_position_name = null;
		}
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		$this->data['banners'] = array();
		$data = array(
			'filter_name'  => $filter_name,
			'filter_position_type'  => $filter_position_type,
			'filter_status'  => $filter_status,
			'filter_position_name'  => $filter_position_name,
			'start' => ($page - 1) * $this->config->get('config_admin_limit'),
			'limit' => $this->config->get('config_admin_limit')
		);
		
		$results_total = $this->model_design_banner->getTotalBanners($data);
		$results = $this->model_design_banner->getBanners($data);
		foreach ($results as $result) {
			$action = array();
			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->link('design/banner/update', 'token=' . $this->session->data['token'] . '&banner_id=' . $result['banner_id'], 'SSL')
			);
			if($result['position_type'] == 1){
				$position_type = $this->language->get('text_type_category');
				$position_name = $result['category_display_name'];
			}elseif ($result['position_type'] == 2){
				$position_type = $this->language->get('text_type_promotion');
				$position_name = $result['promotion_display_name'];
			}else{
				$position_type = $this->language->get('text_home');
				$position_name = $this->language->get('text_home');
			}
			$this->data['banners'][] = array(
				'banner_id' => $result['banner_id'],
				'name'      => $result['name'],
				'position_type'  => $position_type,
				'position_name'  => $position_name,
				'status'    => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),		
				'action'    => $action
			);
		}
		
		$pagination = new Pagination();
		$pagination->total = $results_total;
		$pagination->page = $page;
		$pagination->limit = $this->config->get('config_admin_limit');
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->link('design/banner', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');
		$this->data['pagination'] = $pagination->render();

		$this->data['filter_name'] = $filter_name;
		$this->data['filter_position_type'] = $filter_position_type == null ? -1 : $filter_position_type;
		$this->data['filter_status'] = $filter_status == null ? -1 : $filter_status;
		$this->data['filter_position_name'] = $filter_position_name;
		$this->data['token'] = $this->session->data['token'];

		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['text_no_results'] = $this->language->get('text_no_results');
		$this->data['text_all'] = $this->language->get('text_all');
		$this->data['text_home'] = $this->language->get('text_home');
		$this->data['text_type_category'] = $this->language->get('text_type_category');
		$this->data['text_type_promotion'] = $this->language->get('text_type_promotion');
		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		
		$this->data['column_name'] = $this->language->get('column_name');
		$this->data['column_position_type'] = $this->language->get('column_position_type');
		$this->data['column_position_name'] = $this->language->get('column_position_name');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_action'] = $this->language->get('column_action');	

		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_filter'] = $this->language->get('button_filter');
 
 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}
		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];
			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$this->template = 'design/banner_list.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}

	private function getForm_text() {
		$this->data['heading_title'] = $this->language->get('heading_title');
		
		$this->data['text_home'] = $this->language->get('text_home');
		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_default'] = $this->language->get('text_default');
		$this->data['text_image_manager'] = $this->language->get('text_image_manager');
 		$this->data['text_browse'] = $this->language->get('text_browse');
		$this->data['text_clear'] = $this->language->get('text_clear');
		$this->data['text_type_home'] = $this->language->get('text_type_home');
		$this->data['text_type_category'] = $this->language->get('text_type_category');
		$this->data['text_type_promotion'] = $this->language->get('text_type_promotion');
				
		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_position_type'] = $this->language->get('entry_position_type');
		$this->data['entry_position'] = $this->language->get('entry_position');
		$this->data['entry_status'] = $this->language->get('entry_status');
		
		$this->data['entry_title'] = $this->language->get('entry_title');
		$this->data['entry_link'] = $this->language->get('entry_link');
		$this->data['entry_image'] = $this->language->get('entry_image');
		$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
		
		$this->data['button_save'] = $this->language->get('button_save');
		$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_add_banner'] = $this->language->get('button_add_banner');
		$this->data['button_remove'] = $this->language->get('button_remove');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

  		$this->data['breadcrumbs'] = array();
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => false
   		);
   		$this->data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('heading_title'),
			'href'      => $this->url->link('design/banner', 'token=' . $this->session->data['token'], 'SSL'),
      		'separator' => ' :: '
   		);
							
		$this->data['token'] = $this->session->data['token'];

		$this->load->model('localisation/language');
		$this->data['languages'] = $this->model_localisation_language->getLanguages();
		
		$this->load->model('product/category');
		$this->data['categories'] = $this->model_product_category->getAllCategoriesForSelect(0, '', '', -1, 0);
		
		$this->load->model('sale/promotion');
		$this->data['promotions'] = $this->model_sale_promotion->getPromotionsToSelect();
		
		$this->load->model('tool/image');
		$this->data['no_image'] = $this->model_tool_image->resize('no_image.jpg', 100, 100);		

	}

	private function getForm_insert() {
		$this->data['action'] = $this->url->link('design/banner/insert', 'token=' . $this->session->data['token'], 'SSL');
		$this->data['cancel'] = $this->url->link('design/banner', 'token=' . $this->session->data['token'], 'SSL');
		
		$this->data['name'] = '';
		$this->data['position_type'] = 0;
		$this->data['position_id'] = 0;
		$this->data['status'] = 0;
		$banner_images = array();

		$this->template = 'design/banner_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}

	private function getForm_update() {
		$this->data['action'] = $this->url->link('design/banner/update', 'token=' . $this->session->data['token'] . '&banner_id=' . $this->request->get['banner_id'], 'SSL');
		$this->data['cancel'] = $this->url->link('design/banner', 'token=' . $this->session->data['token'], 'SSL');
		
		$banner_info = $this->model_design_banner->getBanner($this->request->get['banner_id']);

		$this->data['name'] = $banner_info['name'];
		$this->data['position_type'] = $banner_info['position_type'];
		$this->data['position_id'] = $banner_info['position_id'];
		$this->data['status'] = $banner_info['status'];

		$banner_images = $this->model_design_banner->getBannerImages($this->request->get['banner_id']);
		$this->data['banner_images'] = array();
		foreach ($banner_images as $banner_image) {
			if ($banner_image['image'] && file_exists(DIR_IMAGE . $banner_image['image'])) {
				$image = $banner_image['image'];
			} else {
				$image = 'no_image.jpg';
			}
			$this->data['banner_images'][] = array(
				'banner_image_description' => $banner_image['banner_image_description'],
				'link'                     => $banner_image['link'],
				'image'                    => $image,
				'thumb'                    => $this->model_tool_image->resize($image, 100, 100),
				'sort_order'               => $banner_image['sort_order']
			);	
		} 
	
		$this->template = 'design/banner_form.tpl';
		$this->children = array(
			'common/header',
			'common/footer'
		);
				
		$this->response->setOutput($this->render());
	}
}
?>